package com.zh.core;

/**
 * Created by zhaohui on 2019/11/7
 */
public class MinCoins {

    public static int minCoins1(int[] arr, int aim) {
        if (arr == null || arr.length == 0 || aim < 0) {
            return -1;
        }
        int row = arr.length;
        int col = aim + 1;
        int max = Integer.MAX_VALUE;
        int[][] dp = new int[row][col];
        for (int j = 1; j < col; j++) {
//            if (j - arr[0] >= 0 && dp[0][j-arr[0]] != max) {
//                dp[0][j] = dp[0][j-arr[0]] + 1;
//            } else {
//                dp[0][j] = max;
//            }
            if (j % arr[0] == 0) {
                dp[0][j] = j / arr[0];
            } else {
                dp[0][j] = max;
            }
        }
        for (int i = 1; i < row; i++) {
            for (int j = 1; j < col; j++) {
                if (j - arr[i] >= 0 && dp[i][j-arr[i]] != max) {
                    dp[i][j] = Math.min(dp[i-1][j], dp[i][j-arr[i]] + 1);
                } else {
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        return dp[row-1][col-1] == max ? -1 : dp[row-1][col-1];
    }

    public static int minCoins2(int[] arr, int aim) {
        if (arr == null || arr.length == 0 || aim < 0) {
            return -1;
        }
        int col = aim + 1;
        int row = arr.length;
        int max = Integer.MAX_VALUE;
        int[] dp = new int[col];
        for (int j = 1; j < dp.length; j++) {
            if (j % arr[0] == 0) {
                dp[j] = j / arr[0];
            } else {
                dp[j] = max;
            }
        }
        for (int i = 1; i < row; i++) {
            for (int j = 1; j < col; j++) {
                if (j - arr[i] >= 0 && dp[j-arr[i]] != max) {
                    dp[j] = Math.min(dp[j], dp[j-arr[i]] + 1);
                } else {
                    dp[j] = dp[j];
                }
            }
        }
        return dp[dp.length-1] == max ? -1 : dp[dp.length-1];
    }

    public static int minCoins3(int[] arr, int aim) {
        if (arr == null || arr.length == 0 || aim < 0) {
            return -1;
        }
        int col = aim + 1;
        int row = arr.length;
        int max = Integer.MAX_VALUE;
        int[][] dp = new int[row][col];
        for (int j = 1; j < col; j++) {
            if (j == arr[0]) {
                dp[0][j] = 1;
            } else {
                dp[0][j] = max;
            }
        }
        for (int i = 1; i < row; i++) {
            for (int j = 1; j < col; j++) {
                if (j - arr[i] >= 0 && dp[i-1][j-arr[i]] != max) {
                    dp[i][j] = Math.min(dp[i-1][j], dp[i-1][j-arr[i]] + 1);
                } else {
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        return dp[row-1][col-1] == max ? -1 : dp[row-1][col-1];
    }

    public static int minCoins4(int[] arr, int aim) {
        int col = aim + 1;
        int row = arr.length;
        int max = Integer.MAX_VALUE;
        int[] dp = new int[col];
        for (int j = 1 ; j < col; j++) {
            if (j == arr[0]) {
                dp[j] = arr[0];
            } else {
                dp[j] = max;
            }
        }
        for (int i = 1; i < row; i++) {
            for (int j = col - 1; j >= 0; j--) {
                if (j - arr[i] >= 0 && dp[j - arr[i]] != max) {
                    dp[j] = Math.min(dp[j], dp[j - arr[i]]);
                } else {
                    dp[j] = dp[j];
                }
            }
        }
        return dp[col - 1] == max ? -1 : dp[col - 1];
    }

    public static void main(String[] args) {
        int[] arr = {5, 2, 3};
        int aim = 20;
        System.out.println(minCoins1(arr, aim));
        System.out.println(minCoins2(arr, aim));
        arr = new int[]{5, 2, 3};
        aim = 20;
        System.out.println(minCoins3(arr, aim));
        System.out.println(minCoins4(arr, aim));
    }

}
